package cn.xianyang.org.controller;

import cn.xianyang.basic.annotation.PreAuthorize;
import cn.xianyang.basic.util.AjaxResult;
import cn.xianyang.basic.util.LoginContext;
import cn.xianyang.basic.util.PageList;
import cn.xianyang.org.domain.Department;
import cn.xianyang.org.query.DepartmentQuery;
import cn.xianyang.org.service.IDepartmentService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * resultFul风格接口
 */
@RestController //组合注解 controller + responseBody 返回json格式。如果返回页面就单独用controller
@RequestMapping("/dept")
@Api(tags = "部门接口",description = "部门接口详细描述")
public class DepartmentController {
    @Autowired
    private IDepartmentService departmentService;

    /**
     * 查询一条记录  动词查询getMapping
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Department queryById(@PathVariable("id") Long id){
        return departmentService.queryById(id);
    }

    /**
     * 查询全部记录  动词查询getMapping
     */
    @GetMapping
    public List<Department> queryAll(){
        return departmentService.queryAll();
    }

    /**
     * 新增&修改记录  动词查询putMapping
     */
    @PutMapping
    @PreAuthorize(name = "新增和修改部门数据",value = "dept:AddOrUpdate")
    public AjaxResult AddOrUpdate(@RequestBody Department department) {
        try {
            if (department.getId() == null) {
                //id为空就新增
                departmentService.save(department);
            } else {
                departmentService.update(department);
            }
            //不管成功还是失败都提示用户
            return AjaxResult.me();
        } catch (Exception exception) {
            exception.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("抱歉，我们正在殴打程序员，稍后再试");
        }
    }

    /**
     * 删除记录     动词查询deleteMapping
     */
    @DeleteMapping("/{id}")
    @PreAuthorize(name = "删除一条部门数据",value = "dept:deleteById")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            departmentService.removeById(id);
            return AjaxResult.me();
        } catch (Exception exception) {
            exception.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("抱歉，我们正在殴打程序员，稍后再试");
        }
    }

    /**
     * 高级查询+分页     动词查询postMapping
     * @RequestBody 从请求体中获取参数
     */
    @PostMapping
    public PageList<Department> queryPage(@RequestBody DepartmentQuery query, HttpServletRequest request){
        System.out.println(LoginContext.getCurrentLogininfo(request));
        return departmentService.queryPage(query);
    }

    /**
     * 批量删除     动词查询PatchMapping
     */
    @PatchMapping
    @PreAuthorize(name = "批量删除部门数据",value = "dept:batchRemove")
    public AjaxResult batchRemove(@RequestBody List<Long> ids){
        try {
            departmentService.batchRemove(ids);
            return AjaxResult.me();
        } catch (Exception exception) {
            exception.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("抱歉，我们正在殴打程序员，稍后再试");
        }
    }

    @GetMapping("/tree")
    public List<Department> queryTree(){
        return departmentService.queryTree();
    }

}
